package com.example.springmvc_demo3.service.impl;

import com.example.springmvc_demo3.dto.UserDTO;
import com.example.springmvc_demo3.entity.User;
import com.example.springmvc_demo3.mapper.UserMapper;
import com.example.springmvc_demo3.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public void register(UserDTO userDTO) {
        // 验证用户名是否重复
        User existingUser = userMapper.findByUsername(userDTO.getUsername());
        if (existingUser != null) {
            throw new RuntimeException("用户名已存在");
        }
        //把DTO转成实体类
        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(userDTO.getPassword());
        user.setEmail(userDTO.getEmail());
        user.setSex("");
        user.setRole("普通用户");
        //把数据存入数据库
        userMapper.addUser(user);
    }
}